package it.iaproject.ui;

import it.iaproject.Cell;
import it.iaproject.Map;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class Ui extends JFrame {

    
    public static final Color GREEN = new Color(51,153,0);
    public static final Color BLU = new Color(0,0,153);

    private MapPanel mapPanel;
    private JPanel controlPanel;
    private Map map;
    
    private JLabel infoLabel;
    private JLabel nrMovementsLabel;
    private JLabel euristicNrLabel;

    public Ui(Map m){
    	map = m;
    	setTitle("Game");
    	
    	JPanel messagePanel = new JPanel();
    	messagePanel.setLayout(new FlowLayout());

    	infoLabel = new JLabel("In esecuzione");
    	infoLabel.setPreferredSize(new Dimension(160,35));
    	infoLabel.setBorder(BorderFactory.createTitledBorder("Stato del gioco"));
 
    	nrMovementsLabel = new JLabel("0");
    	nrMovementsLabel.setPreferredSize(new Dimension(80,35));
    	nrMovementsLabel.setBorder(BorderFactory.createTitledBorder("# mosse"));

    	euristicNrLabel = new JLabel("0");
    	euristicNrLabel.setPreferredSize(new Dimension(80,35));
    	euristicNrLabel.setBorder(BorderFactory.createTitledBorder("Euristica"));
    	
    	messagePanel.add(infoLabel);
    	messagePanel.add(nrMovementsLabel);
    	messagePanel.add(euristicNrLabel);
   	
    	
    	mapPanel = new MapPanel(m);
    	
    	controlPanel = new JPanel();
		controlPanel.setLayout(new FlowLayout());
		
		
		JButton genNewMapButton = new JButton("Genera Nuova Mappa");
		genNewMapButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				map.generateNewMap();
				mapPanel.setAlreadyInitialized(false);
				mapPanel.revalidate();
				controlPanel.revalidate();
				infoLabel.setText("Gioco in esecuzione");
			}
		});
		
		JButton playPauseButton = new JButton("Play/Pausa");
		playPauseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (map.ia.isPaused()) {
					synchronized(map.ia) {
						map.ia.notify();
						infoLabel.setText("In esecuzione");
					}
				} else {
					map.ia.setPaused(true);
					infoLabel.setText("In pausa");
				}
				mapPanel.revalidate();
				controlPanel.revalidate();
			}
		});
		
		
		controlPanel.add(genNewMapButton);
		controlPanel.add(playPauseButton);
		

		add(messagePanel, BorderLayout.NORTH);
		add(mapPanel, BorderLayout.CENTER);
		add(controlPanel, BorderLayout.SOUTH);
    	
        add(mapPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

	public void updateMapPanel(Cell c) {
		mapPanel.setModifiedCell(c);
		mapPanel.update(mapPanel.getGraphics());
		euristicNrLabel.setText(new Integer(map.ia.getHeuristic()).toString());
		nrMovementsLabel.setText(new Integer(map.ia.getNumOfNodes()).toString());
	}
	
	public void updateMapPanel() {
		mapPanel.update(mapPanel.getGraphics());
		euristicNrLabel.setText(new Integer(map.ia.getHeuristic()).toString());
		nrMovementsLabel.setText(new Integer(map.ia.getNumOfNodes()).toString());
	}

}